import re
import time
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.wait import WebDriverWait

from pom.Admin.BankDetailPage import BankDetailPage
from pom.Admin.QuestionBankPage import QuestionDetailPage
from pom.Admin.TrainingBankPage import AdminBankManage
from pom.base import BaseDriver

class ComBank(BaseDriver):

    def __init__(self, driver):
        super(ComBank, self).__init__(driver)
        self.banklist = AdminBankManage(driver)
        self.bankdetail = BankDetailPage(driver)
        self.questiondetail = QuestionDetailPage(driver)

    # 跳转到竞赛题库
    def jump_competation_bank(self):
        self.Click(self.banklist.bank_manage)
        self.Click(self.banklist.competation_bank)

    def click_title(self, line):
        self.Click(self.banklist.bank_list_data_ele(line, 2))

    # 添加题目
    def add_question(self, question_type, correct_answer, *args):
        time.sleep(1)
        # TODO 为什么显示等待没有起效
        WebDriverWait(self.driver, 2).until(EC.presence_of_element_located(self.bankdetail._add_question_btn))
        self.Click(self.bankdetail.add_question_btn)
        # WebDriverWait(self.driver, 2).until(EC.presence_of_element_located(self.questiondetail._knowledge))
        self.Clearandsend(self.questiondetail.question_title, "题目标题")
        self.Clearandsend(self.questiondetail.question_body, "标题题干")

        # question_type_enum = {
        #     "radio_type":[self.questiondetail.radio_type, self.questiondetail.option(), self.questiondetail.answer_radio],
        #     "select_type":self.questiondetail.select_type,
        #     "fill_blank_type":self.questiondetail.fill_blank_type,
        #     "subject_type":self.questiondetail.subject_type
        # }

        if question_type == "radio_type":
            self.Click(self.questiondetail.radio_type)
            for i in range(1, 3):
                self.Clearandsend(self.questiondetail.option(i), "选项选项")
            self.Click(self.questiondetail.answer_radio(correct_answer))
        elif question_type == "select_type":
            self.Click(self.questiondetail.select_type)
            for i in range(1, 3):
                self.Sendkeys(self.questiondetail.option(i), "选项选项")
            # answer是列表 【1，3】代表AC
            for x in correct_answer:
                self.Click(self.questiondetail.answer_select(x))
        elif question_type == "fill_blank_type":
            self.Click(self.questiondetail.fill_blank_type)
            self.Clearandsend(self.questiondetail.answer_fill(), correct_answer)
            # 勾选影响因素
            ## TODO 影响因素处理

        elif question_type == "subject_type":
            pass
        else:
            raise Exception("创建题目 题目类型错误")


        self.Clearandsend(self.questiondetail.score_setting, 3)
        self.Clearandsend(self.questiondetail.other_desc, "其他说明其他说明")
        self.Click(self.questiondetail.save_btn)

    # 添加分组
    # def add_section(self):
    #     self.Click(self.)

